<!--
 * @Descripttion: 
 * @Author: yuxi
 * @Date: 2022-10-04 22:15:48
 * @LastEditors: yuxi
 * @LastEditTime: 2022-10-05 21:45:00
-->
<template>
  <div class="code">
     <span class="tag" @click="show" v-if="!drawer">示例代码</span>
     <el-drawer 
     title="示例代码" 
     :visible.sync="drawer" 
     direction="rtl" 
     append-to-body
     :modal="false"
     custom-class="code-drawer"	>
      <div class="codes">
        <el-collapse v-model="activeNames">
          <el-collapse-item title="HTML" name="html">
            <div class="markdown-body" ref="htmlcode"   v-if="!changing">
              <pre>
                <code  class="language-html">
                  <component :is="htmlCom" > </component>
                </code>
              </pre>
            </div>
          </el-collapse-item>
          <el-collapse-item title="Javascript" name="js">
            <div class="markdown-body" ref="jscode"  v-if="!changing">
              <pre>
                <code  class="language-javascript">
                  <component :is="jsCom" :key="jsCom"  > </component>
                </code>
              </pre>
            </div>
          </el-collapse-item>
          <el-collapse-item title="Css" name="css">
            <div class="markdown-body" ref="csscode"  v-if="!changing">
              <pre>
                <code class="language-xml">
                  <component :is="cssCom"> </component>
                </code>
              </pre>
            </div>
          </el-collapse-item> 
        </el-collapse>
      </div>
    </el-drawer> 
  </div>
</template>

<script>
import md from '@/docs/ContextMenu/js.md'
import 'highlight.js/styles/github.css'
// import 'github-markdown-css'
let hljs = require('highlight.js');
import './index'

export default {
  components:{
    md
  },
  data() {
    return {
      drawer: false,
      activeNames: ['html'],
      changing: false
    }
  },
  computed:{
    jsCom() {
      return this.$route.name + '_js.md'
    },
    cssCom() {
      return this.$route.name + '_css.md'
    },
    htmlCom() {
      return this.$route.name + '_html.md'
    }
  },
  watch:{
    '$route.name' () {
      this.change()
    }
  },
  mounted () {

  },
  methods:{
    show () {
      this.drawer = true 
      this.reload()
    },
    change () {
      this.changing = true
      this.$nextTick(() => {
        this.changing = false
        this.reload()
      })
    },
    reload () {
      console.log('reload')
      this.$nextTick(() => {
        hljs.initHighlightingOnLoad();
      }) 
    }
  },
  
}
</script>
<style lang="less">
.code {
  position: fixed;
  right: 0px;
  bottom: 100px;
  .tag {
    display: inline-block;
    width: 70px;
    font-size: 15px;
    padding: 3px;
    text-indent: 0;
    text-align: center;
    background-color: white;
    color: #999;
    font-weight: bold;
    border-radius:  3px  3px 0 0;
    transform: rotate(-90deg);
    transform-origin: 52px 0;
    cursor: pointer;
    border: 1px solid #dcdfe6;
  }
}
.code-drawer {
  .el-drawer__header {
    margin: 0;
    padding: 10px;
  }
  .el-collapse-item__header {
    padding: 0 10px;
  }
}
</style>
